<?php
namespace app\proxy\controller;
use think\Db;
use think\Request;

class Qudao extends Base
{
	public function _initialize()
	{
		// 调用父类前置方法
		parent::_initialize();

		// 登录校验
		$this->IsLogin();
	}


	public function index(Request $request)
	{
        $proxy_id = $_SESSION['think']['proxy']['proxy_id'];
        if ($request->isAjax()){
            $param = $request->param();
            if (!empty($param['name'])){
                $name = Db::name('user')->where('nickname','like','%'.$param['name'].'%')->column('id');
                $map['user_id'] = ['in',implode(',',$name)];
            }
            $page = $param['page'];
            $limit = $param['limit'];
            $map['id'] = ['>',-1];
            $map['proxy_id'] = $proxy_id;
            $list = Db::name('qu_apply')->where($map)->page($page)->limit($limit)->order('id desc')->select();
            $count = Db::name('qu_apply')->where($map)->count();
            $status_list = ['待审核','审核通过','拒绝通过'];
            $juese_list = [0=>'待定',1=>'用户',2=>'店铺',3=>'业务员'];
            foreach ($list as $key => $value){
                $list[$key]['add_time_time'] = date('Y-m-d H:i',$value['add_time']);
                $list[$key]['upd_time_time'] = $value['upd_time'] > 0 ? date('Y-m-d H:i',$value['upd_time']) : '-';
                $list[$key]['status_status'] = $status_list[$value['status']];
                $list[$key]['juese_name'] = $juese_list[$value['juese']];
                $list[$key]['user_name'] = Db::name('user')->where('id',$value['user_id'])->value('nickname');
                $list[$key]['user_bl'] = Db::name('user')->where('id',$value['user_id'])->value('share_bl');
                $list[$key]['yewu_name'] = !empty($value['g_uid']) ? Db::name('user')->where('id',$value['g_uid'])->value('nickname') : '-';

                $proxy_name = '';
                if ($value['proxy_id'] != 0){
                    $city = Db::name('proxy')->where('proxy_id',$value['proxy_id'])->find();
                    if ($city['is_xian'] != 0){
                        $ma = $city['qu'];
                    }else{
                        $ma = $city['shi'];
                    }
                    $proxy_name = Db::name('area')->where('id',$ma)->value('short_name');
                }
                $list[$key]['city_name'] = $proxy_name;

            }
            echo json_encode(['code' => 0, 'msg' => 'ok', 'data' => $list,'count'=>$count]);
            die;
        }else{
            return $this->fetch();
        }
    }

    public function fix(Request $request){
        $param = $request->param();
        if (!empty($param['id'])){
            $info = Db::name('qu_apply')->where('id',$param['id'])->find();
            if (!empty($info)){
                $status_list = ['待审核','审核通过','拒绝通过'];
                $juese_list = [0=>'待定',1=>'用户',2=>'店铺',3=>'业务员'];
                $info['add_time_time'] = date('Y-m-d H:i',$info['add_time']);
                $info['status_status'] = $status_list[$info['status']];
                $info['juese_name'] = $juese_list[$info['juese']];
                $info['user_name'] = Db::name('user')->where('id',$info['user_id'])->value('nickname');
                $info['user_bl'] = Db::name('user')->where('id',$info['user_id'])->value('share_bl');
                $info['yewu_name'] = !empty($info['g_uid']) ? Db::name('user')->where('id',$info['g_uid'])->value('nickname') : '-';
                $this->assign('info',$info);
                $yewu_list = Db::name('user')->where('is_qu',1)->where('juese',3)->select();
                $this->assign('yewu_list',$yewu_list);
                return $this->fetch();
            }
        }
    }

    public function do_fix(Request $request){
        $param = $request->param();
        if ($param['status'] == 2){
            if ( $re = Db::name('qu_apply')->where('id',$param['id'])->update(['status'=>2,'upd_time'=>time()])){
                echo json_encode(['code'=>0,'msg'=>'拒绝通过']);
            }else{
                echo json_encode(['code'=>-1,'msg'=>'拒绝失败,请重试']);
            }

        }else{
//            dump($param);die;
            $user_id = explode(',',$param['user_name'])[0];
            if (!empty($param['juese'])){
                $upd['juese'] = explode(',',$param['juese'])[0];

            }
            if (!empty($param['qu_content'])){
                $upd['qu_content'] = $param['qu_content'];
            }
            if (!empty($param['g_uid'])){
                $upd['g_uid'] = $param['g_uid'];
            }

            if (!empty($param['user_bl'])){
                $upd['share_bl'] = $param['user_bl'];
            }
            $upd['is_qu'] = 1;
            $upd['qu_type'] = $param['proxy_id'];

            $user_re = Db::name('user')->where('id',$user_id)->update($upd);
            if ($user_re){
                $result = Db::name('qu_apply')->where('id',$param['id'])->update(['status'=>1,'upd_time'=>time()]);
                if ($result){
                    echo json_encode(['code'=>0,'msg'=>'审核通过']);
                }else{
                    echo json_encode(['code'=>-1,'msg'=>'审核状态失败,请重试']);
                }
            }else{
                echo json_encode(['code'=>-1,'msg'=>'更改状态失败,请重试']);
            }
        }
    }
}